package com.appkubes.fintech.restful.application.user.controller;

import com.appkubes.base.core.entity.vo.UserKeyVO;
import com.appkubes.fintech.common.response.Response;
import com.appkubes.fintech.core.po.FintechUser;
import com.appkubes.fintech.core.po.FintechUserContactExample;
import com.appkubes.fintech.core.po.FintechUserPersonal;
import com.appkubes.fintech.restful.application.user.dao.*;
import com.appkubes.fintech.restful.application.user.domain.UserCertificateDomain;
import com.appkubes.fintech.restful.application.user.entity.dto.UserCertificateStatusDTO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;

/**
 * 用户信息
 */
@Slf4j
@RequestMapping("/user")
@RestController
@Api(tags = "用户信息")
public class UserController {
    @Resource
    UserCertificateDomain certificateService;
    @Resource
    UserDao userDao;
    @Resource
    UserJobDao userJobDao;
    @Resource
    UserBaseInfoDao baseInfoDao;
    @Resource
    UserBankCardDao bankCardDao;
    @Resource
    UserContactDao contactDao;
    @Resource
    UserPersonalDao personalDao;


    @GetMapping("/getCertificateStatus")
    @ApiOperation("用户认证状态信息")
    public Response<UserCertificateStatusDTO> getCertificateStatus(@Valid UserKeyVO userKeyVO, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return Response.fail("参数检查失败");
        }

        UserCertificateStatusDTO userCertificateStatusDTO = new UserCertificateStatusDTO();
        FintechUser fintechUser = certificateService.getUserDao().selectByPrimaryKey(userKeyVO.getUserKey());
        if (fintechUser != null) {
            BeanUtils.copyProperties(fintechUser, userCertificateStatusDTO);
        }

        FintechUserPersonal userPersonal = personalDao.selectByPrimaryKey(userKeyVO.getUserKey());
        if (userPersonal != null) {
            userCertificateStatusDTO.setIdcard_back_view(userPersonal.getIdcardBackView());
            userCertificateStatusDTO.setIdcard_front_view(userPersonal.getIdcardFrontView());
        }


        return Response.success(userCertificateStatusDTO);
    }


    /**
     * 方便测试
     *
     * @param userKeyVO
     * @param bindingResult
     * @return
     */
    @GetMapping("/del")
    @ApiOperation("删除用户信息")
    public Response delUser(@Valid UserKeyVO userKeyVO, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return Response.fail("参数检查失败");
        }
        Integer effectRow = 0;
        String userKey = userKeyVO.getUserKey();
        effectRow += userDao.deleteByPrimaryKey(userKey);
        effectRow += userJobDao.deleteByPrimaryKey(userKey);
        effectRow += baseInfoDao.deleteByPrimaryKey(userKey);
        effectRow += bankCardDao.deleteByPrimaryKey(userKey);

        FintechUserContactExample example = new FintechUserContactExample();
        example.createCriteria().andUserKeyEqualTo(userKey);
        effectRow += contactDao.deleteByExample(example);

        effectRow += personalDao.deleteByPrimaryKey(userKey);

        return Response.success(effectRow);
    }
}
